//
// Created by fu on 2024-07-27.
//
#include "matrix.h"
#include <cuda_runtime.h>
#include <device_launch_parameters.h>

cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size);

cudaError_t mulWithCuda(const int *a, const int *b, int *out, unsigned int size);

__global__ void addKernel(int *c, const int *a, const int *b)
{
    int i = threadIdx.x;
    c[i] = sqrt((float)a[i]) + sqrt((float)b[i]);
}

__global__ void mulKernel(const int *a, const int *b, int *out)
{
    int c = threadIdx.x + threadIdx.y * blockDim.x;
    int k, r;
    for (r = 0; r < 4; r++) {
        out[c * 4 + r] = 0.0f;
        for (k = 0; k < 4; k++) {
            out[c * 4 + r] += a[c * 4 + k] * b[k * 4 + r];
        }
    }
}

void mat4x4_mul_cpu(int *a, int *b, int *out) {
    int k, r, c;
    for (c = 0; c < 4; c++) {
        for (r = 0; r < 4; r++) {
            out[c * 4 + r] = 0.0f;
            for (k = 0; k < 4; k++) {
                out[c * 4 + r] += a[c * 4 + k] * b[k * 4 + r];
            }
        }
    }
}

void mat4x4_identity_cpu(int *out) {
    int i, j;
    for (i = 0; i < 4; i++) {
        for (j = 0; j < 4; j++) {
            out[i * 4 + j] = i == j ? 1 : 0;
        }
    }
}

void mat4x4_mul_gpu(int* a, int* b, int* out, unsigned int arraySize) {
    cudaError_t cudaStatus = mulWithCuda(a, b, out, arraySize);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "mulWithCuda failed!");
    }

    printf("[\n\t[%d,%d,%d,%d],\n"
           "\t[%d,%d,%d,%d],\n"
           "\t[%d,%d,%d,%d],\n"
           "\t[%d,%d,%d,%d]\n]\n",
           out[0], out[1], out[2], out[3], out[4], out[5], out[6], out[7], out[8], out[9], out[10], out[11], out[12], out[13], out[14], out[15]);

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceReset failed!");
    }
}

//int main() {
//    const int arraySize = 5;
//    const int a[arraySize] = { 1, 2, 3, 4, 5 };
//    const int b[arraySize] = { 10, 20, 30, 40, 50 };
//    int c[arraySize] = { 0 };
//
//    // Add vectors in parallel.
//    cudaError_t cudaStatus = addWithCuda(c, a, b, arraySize);
//    if (cudaStatus != cudaSuccess) {
//        fprintf(stderr, "addWithCuda failed!");
//        return 1;
//    }
//
//    printf("{1,2,3,4,5} + {10,20,30,40,50} = {%d,%d,%d,%d,%d}\n",
//           c[0], c[1], c[2], c[3], c[4]);
//
//    // cudaDeviceReset must be called before exiting in order for profiling and
//    // tracing tools such as Nsight and Visual Profiler to show complete traces.
//    cudaStatus = cudaDeviceReset();
//    if (cudaStatus != cudaSuccess) {
//        fprintf(stderr, "cudaDeviceReset failed!");
//        return 1;
//    }
//
//    return 0;
//}

int main() {
    const int arraySize = 16;
    const int a[arraySize] = { 2, 3, 4, 6, 3, 5, 6, 9, 4, 1, 7, 2, 5, 7, 3, 12 };
    const int b[arraySize] = { 3, 5, 3, 7, 9, 3, 9, 3, 3, 8, 3, 3, 6, 16, 27, 30 };
    int c[arraySize] = { 0 };

    // Add vectors in parallel.
    cudaError_t cudaStatus = mulWithCuda(a, b, c, arraySize);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "mulWithCuda failed!");
        return 1;
    }

    printf("[[%d,%d,%d,%d],\n"
           "\t[%d,%d,%d,%d],\n"
           "\t[%d,%d,%d,%d],\n"
           "\t[%d,%d,%d,%d]]\n",
           c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10], c[11], c[12], c[13], c[14], c[15]);

    // cudaDeviceReset must be called before exiting in order for profiling and
    // tracing tools such as Nsight and Visual Profiler to show complete traces.
    cudaStatus = cudaDeviceReset();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceReset failed!");
        return 1;
    }

    return 0;
}

cudaError_t mulWithCuda(const int *a, const int *b, int *out, unsigned int size)
{
    int *dev_a = 0;
    int *dev_b = 0;
    int *dev_out = 0;

    cudaError_t cudaStatus;
    unsigned int len = size * sizeof(int);

    do {
        cudaStatus = cudaSetDevice(0);
        if (cudaStatus != cudaSuccess) {
            break;
        }

        cudaStatus = cudaMalloc((void**)&dev_out, len);
        if (cudaStatus != cudaSuccess) {
            break;
        }

        cudaStatus = cudaMalloc((void**)&dev_a, len);
        if (cudaStatus != cudaSuccess) {
            break;
        }

        cudaStatus = cudaMalloc((void**)&dev_b, len);
        if (cudaStatus != cudaSuccess) {
            break;
        }

        cudaStatus = cudaMemcpy(dev_a, a, len, cudaMemcpyHostToDevice);
        if (cudaStatus != cudaSuccess) {
            break;
        }

        cudaStatus = cudaMemcpy(dev_b, b, len, cudaMemcpyHostToDevice);
        if (cudaStatus != cudaSuccess) {
            break;
        }

        mulKernel<<<4, 4>>>(dev_a, dev_b, dev_out);

        cudaStatus = cudaGetLastError();
        if (cudaStatus != cudaSuccess) {
            break;
        }

        cudaStatus = cudaDeviceSynchronize();
        if (cudaStatus != cudaSuccess) {
            break;
        }

        cudaStatus = cudaMemcpy(out, dev_out, len, cudaMemcpyDeviceToHost);
        if (cudaStatus != cudaSuccess) {
            break;
        }
    } while(0);
Error:
    cudaFree(dev_a);
    cudaFree(dev_b);
    cudaFree(dev_out);
    return cudaStatus;
}

// Helper function for using CUDA to add vectors in parallel.
cudaError_t addWithCuda(int *c, const int *a, const int *b, unsigned int size)
{
    int *dev_a = 0;
    int *dev_b = 0;
    int *dev_c = 0;
    cudaError_t cudaStatus;

    // Choose which GPU to run on, change this on a multi-GPU system.
    cudaStatus = cudaSetDevice(0);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
        goto Error;
    }

    // Allocate GPU buffers for three vectors (two input, one output)    .
    cudaStatus = cudaMalloc((void**)&dev_c, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_a, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    cudaStatus = cudaMalloc((void**)&dev_b, size * sizeof(int));
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMalloc failed!");
        goto Error;
    }

    // Copy input vectors from host memory to GPU buffers.
    cudaStatus = cudaMemcpy(dev_a, a, size * sizeof(int), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    cudaStatus = cudaMemcpy(dev_b, b, size * sizeof(int), cudaMemcpyHostToDevice);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    // Launch a kernel on the GPU with one thread for each element.
    addKernel<<<1, size>>>(dev_c, dev_a, dev_b);

    // Check for any errors launching the kernel
    cudaStatus = cudaGetLastError();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "addKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
        goto Error;
    }

    // cudaDeviceSynchronize waits for the kernel to finish, and returns
    // any errors encountered during the launch.
    cudaStatus = cudaDeviceSynchronize();
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaDeviceSynchronize returned error code %d after launching addKernel!\n", cudaStatus);
        goto Error;
    }

    // Copy output vector from GPU buffer to host memory.
    cudaStatus = cudaMemcpy(c, dev_c, size * sizeof(int), cudaMemcpyDeviceToHost);
    if (cudaStatus != cudaSuccess) {
        fprintf(stderr, "cudaMemcpy failed!");
        goto Error;
    }

    Error:
    cudaFree(dev_c);
    cudaFree(dev_a);
    cudaFree(dev_b);

    return cudaStatus;
}